package cn.iocoder.yudao.module.member.service.mpshunfengcheorderevaluate;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.app.mpshunfengcheorderevaluate.vo.*;
import cn.iocoder.yudao.module.member.convert.mpshunfengcheorderevaluate.MpShunfengcheOrderEvaluateConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.mpshunfengcheorderevaluate.MpShunfengcheOrderEvaluateDO;
import cn.iocoder.yudao.module.member.dal.mysql.mpshunfengcheorderevaluate.MpShunfengcheOrderEvaluateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MP_SHUNFENGCHE_ORDER_EVALUATE_NOT_EXISTS;

/**
 * 顺风车订单评价 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MpShunfengcheOrderEvaluateServiceImpl implements MpShunfengcheOrderEvaluateService {

    @Resource
    private MpShunfengcheOrderEvaluateMapper mpShunfengcheOrderEvaluateMapper;

    @Override
    public Long createMpShunfengcheOrderEvaluate(AppMpShunfengcheOrderEvaluateCreateReqVO createReqVO) {
        // 插入
        MpShunfengcheOrderEvaluateDO mpShunfengcheOrderEvaluate = MpShunfengcheOrderEvaluateConvert.INSTANCE.convert(createReqVO);
        mpShunfengcheOrderEvaluateMapper.insert(mpShunfengcheOrderEvaluate);
        // 返回
        return mpShunfengcheOrderEvaluate.getId();
    }

    @Override
    public void updateMpShunfengcheOrderEvaluate(AppMpShunfengcheOrderEvaluateUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMpShunfengcheOrderEvaluateExists(updateReqVO.getId());
        // 更新
        MpShunfengcheOrderEvaluateDO updateObj = MpShunfengcheOrderEvaluateConvert.INSTANCE.convert(updateReqVO);
        mpShunfengcheOrderEvaluateMapper.updateById(updateObj);
    }

    @Override
    public void deleteMpShunfengcheOrderEvaluate(Long id) {
        // 校验存在
        this.validateMpShunfengcheOrderEvaluateExists(id);
        // 删除
        mpShunfengcheOrderEvaluateMapper.deleteById(id);
    }

    private void validateMpShunfengcheOrderEvaluateExists(Long id) {
        if (mpShunfengcheOrderEvaluateMapper.selectById(id) == null) {
            throw exception(MP_SHUNFENGCHE_ORDER_EVALUATE_NOT_EXISTS);
        }
    }

    @Override
    public MpShunfengcheOrderEvaluateDO getMpShunfengcheOrderEvaluate(Long id) {
        return mpShunfengcheOrderEvaluateMapper.selectById(id);
    }

    @Override
    public List<MpShunfengcheOrderEvaluateDO> getMpShunfengcheOrderEvaluateList(Collection<Long> ids) {
        return mpShunfengcheOrderEvaluateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MpShunfengcheOrderEvaluateDO> getMpShunfengcheOrderEvaluatePage(AppMpShunfengcheOrderEvaluatePageReqVO pageReqVO) {
        return mpShunfengcheOrderEvaluateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MpShunfengcheOrderEvaluateDO> getMpShunfengcheOrderEvaluateList(AppMpShunfengcheOrderEvaluateExportReqVO exportReqVO) {
        return mpShunfengcheOrderEvaluateMapper.selectList(exportReqVO);
    }

    @Override
    public MpShunfengcheOrderEvaluateDO getEvaluate(Long orderId) {
        LambdaQueryWrapper<MpShunfengcheOrderEvaluateDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MpShunfengcheOrderEvaluateDO::getOrderId, orderId);
        List<MpShunfengcheOrderEvaluateDO> mpShunfengcheOrderEvaluateDOS = this.mpShunfengcheOrderEvaluateMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(mpShunfengcheOrderEvaluateDOS)) {
            return mpShunfengcheOrderEvaluateDOS.get(0);
        }
        return null;
    }


}
